//
//  SYOthersDetailsViewController.m
//  TermOfSectionB
//
//  Created by 韩震明 on 15/11/30.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "SYOthersDetailsViewController.h"

#define HEADW self.headView.frame.size.width
#define HEADH self.headView.frame.size.height

@interface SYOthersDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) SYBaseTableView *tableView;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) SYOthersHeaderViewModel *headerModel;
@property (nonatomic, strong) NSMutableArray *cellArr;
@property (nonatomic, strong) NSMutableArray *tagsArr;
@property (nonatomic, strong) NSMutableArray *picsArr;
@property (nonatomic, strong) SYDynamicModel *dynamicModel;
@property (nonatomic, strong) SYAuthorModel *authorModel;
@property (nonatomic, strong) NSMutableArray *dynamicArr;
@property (nonatomic, strong) NSMutableArray *authorArr;

@property(nonatomic, retain) NSMutableArray *earthArr;

@end

@implementation SYOthersDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setNavigation];
    
    [self createTableViewSY];
    
    [self createODData];
}

#pragma mark - TableView Setting

- (void)createTableViewSY
{
    self.tableView = [[SYBaseTableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain superView:self.view delegate:self rowHeight:self.view.frame.size.height separatorStyle:NO showsVerticalScrollIndicator:NO showsHorizontalScrollIndicator:NO header:nil];

    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.tableView registerClass:[SYOthersTVCell class] forCellReuseIdentifier:@"reuse"];
}

#pragma mark - TableView Delegate Method

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cellArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SYOthersTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    [self.authorModel setValuesForKeysWithDictionary:[self.cellArr[indexPath.row] author]];
    [self.dynamicModel setValuesForKeysWithDictionary:[self.cellArr[indexPath.row] dynamic]];
    cell.cellModel = self.cellArr[indexPath.row];
    cell.picModel = self.picsArr[indexPath.row];
    cell.tagsModel = self.tagsArr[indexPath.row];
    cell.authorModel = self.authorModel;
    cell.dynamicModel = self.dynamicModel;
    return cell;
}

#pragma mark - TableView Selected Method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SYBaseWebView *webView = [[SYBaseWebView alloc] init];
    webView.WebViewUrlStr = [self.cellArr[indexPath.row] share_url];
    webView.titleForWeb = @"好物详情";
    [self.navigationController pushViewController:webView animated:YES];
}

#pragma mark - Navigation Setting

- (void)setNavigation
{
    self.navigationItem.title = @"本期话题";
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    leftButton.frame = CGRectMake(0, 0, 40, 40);
    [leftButton addTarget:self action:@selector(leftBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [self.view addSubview:rightButton];
    rightButton.frame = CGRectMake(0, 0, 40, 40);
    [rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    self.navigationController.navigationBar.translucent = NO;
}

#pragma mark - Navigationbar ButtonAction

- (void)leftBarButtonClick:(UIBarButtonItem *)leftBarButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightButtonClick:(UIButton *)rightButton
{
//    [UMSocialSnsService presentSnsIconSheetView:self
//                                         appKey:@"56348b9b67e58ecc6300413f"
//                                      shareText:@"我的"
//                                     shareImage:[UIImage imageNamed:@"icon.png"]
//                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina, UMShareToTencent,UMShareToRenren, UMShareToDouban, UMShareToQzone, UMShareToEmail, UMShareToSms, UMShareToFacebook, UMShareToTwitter, nil]
//                                       delegate:nil];
}

#pragma mark - Create Data

- (void)createODData
{
    NSString *sd = [NSString stringWithFormat:@"%@", self.typeId];
    NSDictionary *bodyView = [NSDictionary dictionaryWithObjectsAndKeys:@"1445332782.662591", @"app_installtime", @"bt_app_ios", @"client_id", @"9c1e6634ce1c5098e056628cd66a17a5", @"client_secret", @"bf8be334eb1fd06b1bf65f5883dd415a", @"oauth_token", @"1242", @"screensize", sd, @"subject_id", @"0", @"page", @"10", @"pagesize", nil];
    NSDictionary *bodyTop = [NSDictionary dictionaryWithObjectsAndKeys:@"1445332782.662591", @"app_installtime", @"bt_app_ios", @"client_id", @"9c1e6634ce1c5098e056628cd66a17a5", @"client_secret", @"bf8be334eb1fd06b1bf65f5883dd415a", @"oauth_token", @"1242", @"screensize", sd, @"subject_id", nil];
    [AFN POSTDataWithURL:@"http://open3.bantangapp.com/community/subject/info" POST:bodyTop block:^(id result) {
        NSDictionary *dic = result;
        self.headerModel = [[SYOthersHeaderViewModel alloc] init];
        [self.headerModel setValuesForKeysWithDictionary:dic[@"data"][@"subject"]];
        if (!self.headerModel) {
            [self createHeadView];
        }
    }];
    
    [AFN POSTDataWithURL:@"http://open3.bantangapp.com/community/subject/listPost" POST:bodyView block:^(id result) {
        NSDictionary *dic = result;
        self.cellArr = [NSMutableArray array];
        self.cellArr = [SYOthersViewModel baseModelByArr:dic[@"data"][@"list"]];
        
        self.tagsArr = [NSMutableArray array];
        self.picsArr = [NSMutableArray array];
        self.dynamicModel = [[SYDynamicModel alloc] init];
        self.authorModel = [[SYAuthorModel alloc] init];
        
        for (NSInteger i = 0; i < self.cellArr.count; i++) {
            NSMutableArray *tagArr = [NSMutableArray array];
            tagArr = [SYAuthorModel baseModelByArr:[self.cellArr[i] otherViewTags]];
            [self.tagsArr addObjectsFromArray:tagArr];
            NSMutableArray *picArr = [NSMutableArray array];
            picArr = [SYPicsModel baseModelByArr:[self.cellArr[i] pics]];
            [self.picsArr addObjectsFromArray:picArr];
        }
        
        if ([self.cellArr count] > 0) {
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - HeadView

- (void)createHeadView
{
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HEADW, HEADH / 2)];
    self.tableView.tableHeaderView = self.headView;
    
    UIImageView *topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, HEADW, 7 * HEADH / 15)];
    [topImageView sd_setImageWithURL:[NSURL URLWithString:[self.headerModel pic2]] placeholderImage:[UIImage imageNamed:@"back.gif"]];
    [self.headView addSubview:topImageView];
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 7 * HEADH / 15, HEADW - 10, HEADH / 6)];
    timeLabel.text = [self.headerModel datestr];
    [self.headView addSubview:timeLabel];
    timeLabel.textColor = [UIColor grayColor];
    timeLabel.font = [UIFont systemFontOfSize:13.0];
    
    UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 9 * HEADH / 15, HEADW - 10, HEADH - HEADH / 6 - 7 * HEADH / 15)];
    descLabel.text = [self.headerModel desc];
    descLabel.textColor = [UIColor grayColor];
    descLabel.font = [UIFont systemFontOfSize:13.0];
    descLabel.numberOfLines = 0;
    [descLabel sizeToFit];
    [self.headView addSubview:descLabel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
